﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AppCode_Employee_OrderInsurance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userNameEmployee"] == null)
            {
                Response.Redirect("../Home/HomePage.aspx");
            }
            else
            {

                int policyId = Convert.ToInt32(Request.QueryString["policyId"]);
                String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
                SqlConnection sqlConnect = new SqlConnection(connect);
                sqlConnect.Open();
                SqlCommand sqlCommand = new SqlCommand("select policyId,policyName,PolicyDesc,Amount,Emi,Policies.CompanyId,companyName from Policies join CompanyDetails" +
                                                        " on CompanyDetails.CompanyId=Policies.CompanyId where policyId=" + policyId, sqlConnect);
                SqlDataReader reader = sqlCommand.ExecuteReader();
                reader.Read();
                txtPolicyId.Text = reader.GetInt32(0).ToString();
                txtPolicyName.Text = reader.GetString(1);
                txtpolicyAmount.Text = reader.GetDecimal(3).ToString();
                txtEmi.Text = reader.GetDecimal(4).ToString();
                txtCompanyId.Text = reader.GetInt32(5).ToString();
                txtCompanyName.Text = reader.GetString(6);
                sqlConnect.Close();

            }
        }
        Label10.Visible = false;
        success.Visible = false;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Boolean check = false;
        if (txtRequestDate.Text.Equals(""))
        {
            Label10.Visible = true;
            check = true;
        }
        if (!check)
        {
            String connect = WebConfigurationManager.ConnectionStrings["ConnectDatabase"].ToString();
            SqlConnection sqlConnect = new SqlConnection(connect);
            sqlConnect.Open();
            SqlCommand sqlCommand = new SqlCommand("Select Empno from EmpRegister where username='" + Session["userNameEmployee"].ToString() + "'", sqlConnect);
            SqlDataReader reader = sqlCommand.ExecuteReader();
            reader.Read();
            int empNo = reader.GetInt32(0);
            reader.Close();
            sqlCommand = new SqlCommand("Insert Into PolicyRequestDetails(requestDate,empNo,policyId) Values(@requestDate,@empNo,@policyId)", sqlConnect);
            sqlCommand.Parameters.AddWithValue("@requestDate", txtRequestDate.Text);
            sqlCommand.Parameters.AddWithValue("@empNo", empNo);
            sqlCommand.Parameters.AddWithValue("@policyId", Convert.ToInt32(txtPolicyId.Text));
            sqlCommand.ExecuteNonQuery();
            sqlConnect.Close();
            ClearTextBox();
            success.Visible = true;
            Button1.Enabled = false;
            
        }
    }
    public void ClearTextBox()
    {
        txtRequestDate.Text = "";
        txtPolicyName.Text = "";
        txtPolicyId.Text = "";
        txtpolicyAmount.Text = "";
        txtEmi.Text = "";
        txtCompanyName.Text = "";
        txtCompanyId.Text = "";
    }

}